//begin of index_new.js
var pages = pages || {};
(function(p) {
  p.index_new = {
    expose: function() {
      //angular 控制器
      window.ArticleList = _articleList;
      _car();
    }
  };
  //functions and localresources
  function _articleList($scope) {
    $.ajax({
      url: 'api.asp',
      type: 'POST',
      data: {
        "action": "getJsonListTop",
        "top": "20"
      },
      dataType: 'json',
      timeout: 1000,
      async: false,
      error: function() {
        setTimeout(_articleList($scope), 500);
      },
      success: function(result) {
        //do some thing is better
        $scope.funclist = result;
      }
    });

    $.ajax({
      url: 'api.asp',
      type: 'POST',
      data: {
        "action": "article.toplikes",
        "top": "20"
      },
      dataType: 'json',
      timeout: 1000,
      async: false,
      error: function() {
        setTimeout(_articleList($scope), 500);
      },
      success: function(result) {
        //do some thing is better
        $scope.mostliked = result;
      }
    });

    $scope.del = function(id) {
      if (!confirm("是否真的删除" + $("#func_" + id).find("td[content='函数名']").val())) {
        return;
      }
      $.ajax({
        url: "api.asp",
        type: "post",
        data: {
          "action": "delItem",
          "id": id
        },
        success: function(data) {
          var oRet = pages.common.parseJSON(data);
          if (oRet) {
            if (oRet.ref == "ok") {
              $("#func_" + id).hide(1000);
            }
          }
        }
      });
    };
  }
  //car display
  function _car() {
    $(function() {
      var car = pages.common.json2html({
        'tagName': 'img',
        'attributes': {
          src: 'http://www.jisuxueche.com/images/car.png',
          style: 'display: none;position: fixed;cursor: pointer;'
        }
      });

      var logo = pages.common.json2html({
        'tagName': 'img',
        'attributes': {
          src: 'http://www.jisuxueche.com/images/logo.png',
          style: 'display: none;position: fixed;cursor: pointer;'
        }
      });

      $(document.body).append(car);
      $(document.body).append(logo);
      $(car).on('click', function() {
        _nav_to_jsxc();
      });
      $(logo).on('click', function() {
        _nav_to_jsxc();
      });
      //car at right
      var jCar = $(car);
      var clientWidth = window.innerWidth;
      var clientHeight = window.innerHeight;
      var picWidth = 214;
      var picHeight = 96;
      jCar.css('left', clientWidth);
      jCar.css('top', (clientHeight - picHeight) / 2);
      jCar.show();
      //car goto center
      jCar.animate({
        left: (clientWidth - picWidth) / 2
      }, 1500, function() {
        $(logo).show();
        $(logo).css('left', (clientWidth - picWidth) / 2 + 71);
        $(logo).css('top', (clientHeight - picHeight) / 2 - 73);
        $(logo).animate({
          top: (clientHeight - picHeight) / 2
        }, 500, function() {
          $(logo).fadeOut(3800);
        });
        setTimeout(function() {
          jCar.animate({
            left: 0 - picWidth
          }, 2500,function() {
            $(car).remove();
            $(logo).remove();
          });
        }, 3000);
      });
    });
  }
  //nav to jisuxueche
  function _nav_to_jsxc() {
    window.open('http://www.jisuxueche.com');
  }
})(pages);

pages.index_new.expose();
//end of index_new.js